#ifndef RAY_QUERY_GLSL
#define RAY_QUERY_GLSL


float QueryVisibility(vec3 origin, vec3 direction, float tMax, uint rayFlags)
{
    float tMin = 0.01f;
 
    // Initializes a ray query object but does not start traversal
    rayQueryEXT rayQuery;

    rayQueryInitializeEXT(rayQuery,
                          topLevelAS,
                          rayFlags,
                          0xFF,
                          origin,
                          tMin,
                          direction,
                          tMax);

    // Start traversal: return false if traversal is complete
    while (rayQueryProceedEXT(rayQuery)) {}

    // Returns type of committed (true) intersection
    if (rayQueryGetIntersectionTypeEXT(rayQuery, true) != gl_RayQueryCommittedIntersectionNoneEXT)
        return 0.0f;

    return 1.0f;
}


float QueryDistance(vec3 origin, vec3 direction, float tMax)
{
    float tMin     = 0.01f;
    uint  rayFlags = gl_RayFlagsOpaqueEXT | gl_RayFlagsTerminateOnFirstHitEXT;

    // Initializes a ray query object but does not start traversal
    rayQueryEXT rayQuery;

    rayQueryInitializeEXT(rayQuery,
                          topLevelAS,
                          rayFlags,
                          0xFF,
                          origin,
                          tMin,
                          direction,
                          tMax);

    // Start traversal: return false if traversal is complete
    while (rayQueryProceedEXT(rayQuery)) {}

    // Returns type of committed (true) intersection
    if (rayQueryGetIntersectionTypeEXT(rayQuery, true) != gl_RayQueryCommittedIntersectionNoneEXT)
        return rayQueryGetIntersectionTEXT(rayQuery, true) < tMax ? 0.0f : 1.0f;

    return 1.0f;
}


#endif